﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GeoUtils
{
    public class GeoEntityChain
    {
        public List<GeoEntity> geoEntityList;
        public List<Token> geoTokenList;

        public GeoEntityChain()
        {
            geoEntityList = new List<GeoEntity>();
            geoTokenList = new List<Token>();
        }

        public GeoEntityChain CopyTo()
        {
            GeoEntityChain geoEntityChain = new GeoEntityChain();
            geoEntityChain.geoEntityList.AddRange(geoEntityList);
            geoEntityChain.geoTokenList.AddRange(geoTokenList);

            return geoEntityChain;
        }

        public int Size()
        {
            return geoEntityList.Count;
        }

        //Sort geo-entity in one chain by level
        public void Sort()
        {
            SortedDictionary<int, GeoEntityChain> sdict = new SortedDictionary<int, GeoEntityChain>();
            for (int i = 0; i < Size(); i++)
            {
                if (sdict.ContainsKey(geoEntityList[i].iLevel) == false)
                {
                    sdict.Add(geoEntityList[i].iLevel, new GeoEntityChain());
                }
                sdict[geoEntityList[i].iLevel].geoEntityList.Add(geoEntityList[i]);
                sdict[geoEntityList[i].iLevel].geoTokenList.Add(geoTokenList[i]);
            }

            geoEntityList.Clear();
            geoTokenList.Clear();
            foreach (KeyValuePair<int, GeoEntityChain> pair in sdict)
            {
                for (int i = 0; i < pair.Value.Size(); i++)
                {
                    geoEntityList.Add(pair.Value.geoEntityList[i]);
                    geoTokenList.Add(pair.Value.geoTokenList[i]);
                }
            }
        }
    }
}
